<?php
/**
* @software         [JiJinCMS]
* @author           [上海集锦信息科技有限公司]
* @website          [http://www.jijinweb.com]
* @email            [sunny@jijinweb.com]
**/
namespace app\admin\controller;
use think\facade\View;
use app\admin\model\RoleModel;
use think\facade\Db;
use think\facade\Request;
class User extends \app\admin\controller\Admin{
    public function __construct(\think\App $app) {
        parent::__construct($app);
        $this->model=new \app\admin\model\UserModel();
        $this->validate=new \app\admin\validate\UserValidate();
    }  
    public function index(){
        $data=$this->model::with('role')->select();
        View::assign("data",$data);
        return View::fetch();
    }
    //设置状态是否启用
    public function setStatus(){
        $id=Request::param('id',0,"intval");
        $status=Request::param('status',0,"intval");
        $this->model->update(['user_id'=>$id,'status'=>$status]);
        //记录日志
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$id,"设置用户状态");          
        $this->success("设置成功");
    }      
    public function add(){
        if(request()->isPost()){
            $data=$this->request->param();
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }
            $data['salt']=make_random();
            $data['password']=md6($data['password'],$data['salt']);
            $this->model->save($data);
            $lastId=$this->model->user_id;
            //更新中间表
            if(Request::param('role_id')){
                $obj=$this->model->find($lastId);
                $obj->role()->saveAll(Request::param('role_id'));
            }
            //
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$lastId,$this->request->param());
            $this->success("添加成功");               
        }else{
            //角色列表
            $role=RoleModel::select();
            View::assign("role",$role);
            return View::fetch();
        }        
    }    
    public function update(){
        if(request()->isPost()){
            $data=$this->request->param();
            //编辑状态下重置username.unique规则（排出当前user_id下的username查重）
            $this->validate->rule['username']['unique']='user,username,'.Request::param('user_id',0,"intval");
            //不修改密码的时候不验证这两个字段
            if(!Request::param('password')){
                unset($this->validate->rule['password']);
                unset($this->validate->rule['repassword']);
            }
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }   
            if(!Request::param('password')){
               unset($data['password']); 
            }else{
                $data['salt']=make_random();
                $data['password']=md6(Request::param('password'),$data['salt']);
            }
            $data['status']=isset($data['status'])?1:0;
            //更新中间表
            Db::name("role_user")->where("user_id",$data['user_id'])->delete();
            $obj=$this->model->find($data['user_id']);
            if(Request::param('role_id')){
                $obj->role()->saveAll(Request::param('role_id'));
            }
            //
            $this->model->update($data);
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$data['user_id'],$this->request->param());
            $this->success("添加成功");
        }  else {
            $user_id=Request::param('user_id',0,"intval");
            $r=$this->model->find($user_id);
            $had_role=Db::name("role_user")->where("user_id",$user_id)->column('role_id');
            //
            $role=RoleModel::select();
            foreach($role as $key=> $v){
                if(in_array($v['id'],$had_role)){
                    $role[$key]["is_checked"]=1;
                }else{
                    $role[$key]["is_checked"]=0;
                }
            }
            View::assign("r",$r);
            View::assign("role",$role);
            return View::fetch();
        }     
    }
    //修改密码
    public function password(){
        if(request()->isPost()){
            $user = $this->model->find($this->user_id);
            $data=$this->request->param();
            if(!$this->validate->scene("updatePass")->check($data)){
                $this->error($this->validate->getError());
            }
            //
            $user = $this->model->find($this->user_id);
            //检验密码
            if($user["password"]!=md6($data["old_password"],$user['salt'])){
                $this->error("原密码不正确");
            } 
            //
            $saveData['user_id']=$this->user_id;
            $saveData['salt']=make_random();
            $saveData['password']=md6($data['password'],$saveData['salt']);
            $this->model->update($saveData);
            //
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$user['user_id']);
            $this->success("修改成功");
        }  else {
            return View::fetch();
        }    
    }
    //删除
    public function delete(){
       $user_id=Request::param('user_id');
       if($user_id==config('app.super_admin_user_id')){
         $this->error("超级管理员禁止删除");  
       }
       if($user_id){
           //删除suer表
           $this->model->destroy($user_id);
           //删除role_user表
           Db::name("role_user")->where("user_id",$user_id)->delete();
           $this->UserLogModel->add(Request()->action(),$this->model->getName(),$user_id);
           $this->success("操作成功");
       }              
    }

}